import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import errorManager from '@ohos.app.ability.errorManager';
import appRecovery from '@ohos.app.ability.appRecovery';

export default class EntryAbility extends UIAbility {
  storage:LocalStorage;
  para:Record<string, number> = { 'showNumber': 3 };
  registerId:number
  // callback = {
  //   onUnhandledException(errMsg){
  //     console.log(errMsg);
  //     appRecovery.saveAppState();//调用后会触发MainAbility的onSaveState(state, wantParams)函数回调
  //     appRecovery.restartApp();
  //
  //   }
  // }

  funcName = (account:number) =>{
    console.log(account+'');
  }
  /**
   * 函数的剩余参数
   * @param firstName
   * @param restOfName
   * @returns
   */
  getEmployeeName(firstName:string,...restOfName:string[]){
    return firstName+''+restOfName.join('')
  }

  /**
   * 可选参数
   * @param firstName
   * @param lastName
   * @returns
   */
  buildName(firstName:string,lastName?:string){
    if (lastName) {
      return firstName+''+lastName;
    } else {
      return firstName;
    }
  }


  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    //应用恢复
    // globalThis.abilityWant = want
    // if (launchParam.launchReason = AbilityConstant.LaunchReason.APP_RECOVERY) {
    //   this.storage = new LocalStorage();
    //   let recoveryData = want.parameters["myData"];
    //   this.storage.setOrCreate("myData",recoveryData);
    //   this.context.restoreWindowStage(this.storage);
    // }
    // //应用恢复end
    // let employeeName1 = this.getEmployeeName('Tom')
    // let employeeName2 = this.getEmployeeName('Tom','Sandy','Lucas')

    this.storage = new LocalStorage(this.para)
    let list1=[4,5,6];
    // for(let i in list1){
    //   console.log(i);
    // }


    for (let index = 0; index < list1.length; index++) {
      const element = list1[index];
    }

    for(let i of list1){
      console.log(i+'');
    }
    //跳转
    const that = this
    this.context.eventHub.on("getAbilityData",(data)=>{
      let detailWant =  {
        deviceId: '', // deviceId为空表示本设备
        bundleName: 'om.example.myapplication',
        abilityName: 'NewsDetailAbility',
        moduleName: 'entry', // moduleName非必选
        // parameters: { // 自定义信息
        //   instanceKey: "ddf1132",
        // },
      }
      data.context = that.context
      data.launchWant = detailWant
    })
  }

  onSaveState(want, wantParams){
    console.log("onSaveState");
    // wantParams["myData"] = "zkq";
    return AbilityConstant.OnSaveResult.ALL_AGREE;
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
    this.context.eventHub.off("getAbilityData")
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    // globalThis.registerObserver =(()=>{
    //   this.registerId = errorManager.on('error',this.callback);
    //
    // })


    windowStage.loadContent('pages/MainPage',this.storage, (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });


  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
    // globalThis.unRegisterObserver = (()=>{
    //     errorManager.off('error',this.registerId,(err)=>{
    //       console.error("[Demo] err:", err);
    //     });
    // });
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
